package com.nosweattechnet.placemap;

import java.util.concurrent.ExecutionException;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.MeasureSpec;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.nosweattechnet.datatypes.Place;
import com.nosweattechnet.mapmyway.R;
import com.nosweattechnet.server.PlaceIDAsyncTask;

//Activity to view a map of a Place and Items on the map
public class PlaceMapActivity extends Activity {
	private Place place;
	private String placeid;
	private String userid;
	private PlaceMap pmap;
	private RelativeLayout rl;
	private MapScaler mapscaler;
	private RelativeLayout parentView;
	RelativeLayout.LayoutParams paramsmap=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Gets placeid it was given
        Intent intent= getIntent();
        placeid=intent.getStringExtra(getString(R.string.place_id_intent));
        PlaceIDAsyncTask piat=new PlaceIDAsyncTask(placeid);
        piat.execute();
        
        userid=intent.getStringExtra(getString(R.string.user_id_intent));
        
        DisplayItems.clear();
        DisplayItems.setUserID(userid);
        DisplayItems.setPlaceID(placeid);
        
        //Inflates layout
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        //Adds relative layout that will hold map and ItemMarkers
        rl=new RelativeLayout(this);
        int layout=R.layout.activity_place_map;
        parentView=(RelativeLayout) inflater.inflate(layout,null);
        RelativeLayout.LayoutParams paramsparent=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        paramsparent.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        paramsparent.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        paramsparent.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        paramsparent.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        parentView.addView(rl,paramsparent);
        setContentView(parentView);
        
        //Finds the height and width of screen
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE))
        .getDefaultDisplay().getMetrics(metrics);
        
        float swidth = metrics.widthPixels; 
        float sheight = metrics.heightPixels;
        parentView.measure(MeasureSpec.makeMeasureSpec((int)swidth, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec((int)sheight, MeasureSpec.AT_MOST));
        try {
			place=piat.getPlace();
		} catch (InterruptedException e) {
		} catch (ExecutionException e) {
		}
        //creates object which holds the PlaceMap and does scaling for ItemMarkers
        mapscaler=new MapScaler(place,this,rl.getMeasuredWidth(),rl.getMeasuredHeight(),swidth,sheight);
    	pmap=mapscaler.getPlaceMap();
    	rl.addView(pmap,paramsmap);
    	
    	//Forces parent to measure itself
    	parentView.measure(MeasureSpec.makeMeasureSpec((int)swidth, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec((int)sheight, MeasureSpec.AT_MOST));
	       
    }
    
    //Opens the AddItemActivity
    private void openAddItemActivity(){
    	MapScalerHolder.setMapScaler(mapscaler);
    	Intent intent = new Intent(this, AddItemActivity.class);
    	intent.putExtra(getString(R.string.place_id_intent),placeid);
    	rl.removeView(pmap);
    	startActivity(intent);
    }
    
    protected void openAddItemLocationActivity(String item_id){
    	MapScalerHolder.setMapScaler(mapscaler);
    	Intent intent = new Intent(this, AddItemLocationActivity.class);
    	intent.putExtra(getString(R.string.item_id_intent),item_id);
    	rl.removeView(pmap);
    	startActivity(intent);
    }
    
    @Override
    //Shows all Items in current Array everytime the App is resumed
    public void onResume(){
    	super.onResume();
    	showItems();
    }

	//Standard options menu creation
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_place_map, menu);
        return true;
    }

    //When item on standard options menu is selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_search:
            	onSearchRequested();
            	return true;
            case R.id.itemlist:
            	showItemList();
            	return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            case R.id.removeAll:
            	DisplayItems.clear();
            	this.showItems();
            	return true;
            case R.id.addItem:
            	this.openAddItemActivity();
            	default:
            		return super.onOptionsItemSelected(item);
            	
        }
    }

    private void showItemList(){
    	Intent intent=new Intent(this,ItemListActivity.class);
    	intent.putExtra(getString(R.string.place_id_intent), placeid);
    	intent.putExtra(getString(R.string.user_id_intent),userid);
    	this.startActivity(intent);
    }
    
    //Removes all markers, then adds one for each Item in the Array
    public void showItems(){
    	rl.removeAllViews();
    	rl.addView(pmap,paramsmap);
    	for(int i=0;i<DisplayItems.size();++i){
    		ItemMarker im = DisplayItems.getItemMarker(this, i);
    		RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        	params.leftMargin=(int)(mapscaler.computeItemXCoordiantes(DisplayItems.getXPos(i))-im.XOffSet());
        	params.topMargin=(int)(mapscaler.computeItemYCoordiantes(DisplayItems.getYPos(i))-im.YOffSet());
        	rl.addView(im,params);
    	}
        setContentView(parentView);   	
    }
}
